package com.hdoit.apollo.web.shop.providers;

import java.util.ArrayList;
import java.util.List;

import com.hdoit.apollo.logical.shop.model.Supplier;
import com.hdoit.apollo.logical.shop.service.ISupplierService;
import com.hdoit.apollo.logical.viewmodel.shop.SupplierSearch;
import com.tennetcn.core.common.message.ListItem;
import com.tennetcn.core.common.utils.ChineseUtils;
import com.tennetcn.core.common.utils.CommonApplicationContextUtil;
import com.tennetcn.web.component.attribute.provider.ISingleOptionListProvider;

public class SupplierProvider implements ISingleOptionListProvider<ListItem>{


	ISupplierService supplierService=CommonApplicationContextUtil.getCurrentContext().getBean(ISupplierService.class);
	
	private List<ListItem> dataList;

	@Override
	public List<ListItem> getDataList(Object viewData) {
		SupplierSearch search=new SupplierSearch();
		List<Supplier> suppliers = supplierService.queryListMP(search, null);
		
		if(suppliers==null||suppliers.size()<=0){
			return null;
		}
		dataList=new ArrayList<ListItem>();
		ListItem listItem=null;
		for (Supplier supplier : suppliers) {
			listItem=new ListItem();
			
	        String letter=ChineseUtils.getPinYinHeadChar(supplier.getSupplierName()).toUpperCase();
			listItem.setText(supplier.getSupplierName()+" "+letter);
			listItem.setValue(supplier.getSupplierId());
			
			dataList.add(listItem);
		}
		return dataList;
	}

	@Override
	public ListItem getSelectedItem(Object value, Object viewData) {
		if(value==null){return null;}
		if(dataList==null){return null;}
		for (ListItem listItem : dataList) {
			if(listItem.getValue().equals(value)){
				return listItem;
			}
		}
		return null;
	}
}
